﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

using Happy.Dto;
using Happy.Query;
using Happy.Application.Domain;
using Happy.Application.Query;

namespace Happy.Web.Mvc.ExtJs
{
    /// <summary>
    /// 聚合根控制器。
    /// </summary>
    public abstract class AggregateRootController<TViewModel, TId> : ExtJsController
        where TViewModel : class, IId<TId>
    {
        /// <summary>
        /// 创建。
        /// </summary>
        [ValidateInput(false)]
        public ActionResult Create(TViewModel item)
        {
            this.ApplicationService.Create(item);

            return this.NewtonsoftJson(new
            {
                success = true,
                items = this.LoadModel(item.Id)
            });
        }

        /// <summary>
        /// 修改。
        /// </summary>
        [ValidateInput(false)]
        public ActionResult Update(TViewModel item)
        {
            this.ApplicationService.Update(item);

            return this.NewtonsoftJson(new
            {
                success = true,
                items = this.LoadModel(item.Id)
            });
        }

        /// <summary>
        /// 删除。
        /// </summary>
        [ValidateInput(false)]
        public ActionResult Delete(TViewModel item)
        {
            this.ApplicationService.Delete(item);

            return this.NewtonsoftJson(new
            {
                success = true
            });
        }

        /// <summary>
        /// 获取分页结果，面向支持分页的表格。
        /// </summary>
        public ActionResult Page(QueryRequest query)
        {
            var pageResult = this.PageModels(query);

            return this.PageResult(pageResult.Items, pageResult.TotalItems);
        }

        /// <summary>
        /// 获取分页结果，面向支持分页的表格。
        /// </summary>
        protected virtual PageResult<TViewModel> PageModels(QueryRequest query)
        {
            return this.QueryService.Page(query);
        }

        /// <summary>
        /// 获取一行数据，面向表单。
        /// </summary>
        public ActionResult Load(TId id)
        {
            var item = this.LoadModel(id);

            return this.SingleResult(item);
        }

        /// <summary>
        /// 获取一行数据，面向表单。
        /// </summary>
        protected virtual TViewModel LoadModel(TId id)
        {
            return this.QueryService.SingleOrDefault(id);
        }

        /// <summary>
        /// 获取列表数据，面向不支持分页的表格或下拉框。
        /// </summary>
        public ActionResult Fetch(QueryRequest query)
        {
            var items = this.FetchModels(query);

            return this.FetchResult(items);
        }

        /// <summary>
        /// 获取列表数据，面向不支持分页的表格或下拉框。
        /// </summary>
        protected virtual IEnumerable<TViewModel> FetchModels(QueryRequest query)
        {
            return this.QueryService.Fetch(query);
        }

        /// <summary>
        /// 查询服务。
        /// </summary>
        protected IAggregateRootService<TViewModel> ApplicationService
        {
            get
            {
                return this.Service<IAggregateRootService<TViewModel>>();
            }
        }

        /// <summary>
        /// 查询服务。
        /// </summary>
        protected IQueryService<TViewModel, TId> QueryService
        {
            get
            {
                return this.Service<IQueryService<TViewModel, TId>>();
            }
        }
    }

    /// <summary>
    /// 聚合根控制器。
    /// </summary>
    public abstract class AggregateRootController<TViewModel> : AggregateRootController<TViewModel, Guid>
        where TViewModel : class,IId<Guid>
    {
    }
}
